package com.sc.byahi.user.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;

public class PhotoViewDiv extends Composite {

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	interface MyUiBinder extends UiBinder<Widget, PhotoViewDiv> {
	}

	@UiField
	Image image1;
	@UiField
	Image image2;
	@UiField
	Image image3;
	@UiField
	Image image4;
	@UiField
	Image centerImage;
	
	private Image currImageThumb = null;
	
	public PhotoViewDiv() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	private List<String> imageUrlOrBlobKeys = new ArrayList<String>();
	public PhotoViewDiv(List<String> imageUrlOrBlobKeys){
		this();
		if (imageUrlOrBlobKeys.isEmpty())
			return;
		else
			this.imageUrlOrBlobKeys = imageUrlOrBlobKeys;

		List<Image> thumbs = new ArrayList<Image>();
		thumbs.add(image1);
		thumbs.add(image2);
		thumbs.add(image3);
		thumbs.add(image4);
		
		for (int i = 0; i < this.imageUrlOrBlobKeys.size(); i++) {
			String url = UIUtil.getImageURLs(this.imageUrlOrBlobKeys, i, true);
			Image thumbImage = thumbs.get(i);
			thumbImage.setUrl(url);
			thumbImage.setVisible(true);
			final String currBigImageURL = UIUtil.getImageURLs(this.imageUrlOrBlobKeys, i, false);
			thumbImage.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					currImageThumb = (Image) event.getSource();
					centerImage.setUrl(currBigImageURL);
				}
			});
			if (i == 0) {
				currImageThumb = thumbImage;
			}
		}
		centerImage.setUrl(UIUtil.getImageURLs(imageUrlOrBlobKeys, 0, false));

		FlowPanel buttonPanel = new FlowPanel();
		ToggleButton toggleButton = new ToggleButton("Click on nose");
		buttonPanel.add(toggleButton);
		toggleButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				HandlerRegistration handlerReg = null;
				if (((ToggleButton)event.getSource()).isDown()) {
					handlerReg = centerImage.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {

							int x = event.getX();
							int y = event.getY();

							int width = centerImage.getWidth();
							int height = centerImage.getHeight();

							if ((x - 75) < 0) {
								x = 75;
							}
							if ((x + 75) > width) {
								x = width - 75;
							}
							if ((y - 75) < 0) {
								y = 75;
							}
							if ((y + 75) > height) {
								y = height - 75;
							}

							int topLeftX = x - 75;
							int topLeftY = y - 75;
							int bottomRightX = x + 75;
							int bottomRightY = y + 75;
							currImageThumb.setUrlAndVisibleRect(centerImage.getUrl(), topLeftX, topLeftY, 150, 150);
						}
					});
				} else {
					if (handlerReg != null)
						handlerReg.removeHandler();
				}
			}
		});
//		buttonPanel.add(toggleButton);

		Button delete = new Button("Delete");
		delete.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

			}
		});
	}
	
}